<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.title" clearable class="filter-item" style="width: 200px;" :placeholder="$t('product.controller.list.controllerPlaceholder')" />
      <el-select
        v-model="listQuery.productId"
        style="width: 200px"
        class="filter-item"
        :placeholder="$t('product.controller.list.productPlaceholder')"
        @change="getList"
      >
        <el-option v-for="item in productList" :key="item.id" :label="item.title" :value="item.id" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('product.controller.list.search') }}</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">{{ $t('product.controller.list.create') }}</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      :empty-text="emptyText"
      size="small"
      :element-loading-text="$t('product.controller.list.searching')"
      border
      fit
      highlight-current-row
    >

      <el-table-column align="center" :label="$t('product.controller.list.id')" width="80" prop="id" />

      <el-table-column align="center" :label="$t('product.controller.list.productId')" width="80" prop="productId" />

      <el-table-column align="center" :label="$t('product.controller.list.title')" min-width="100" prop="title" />

      <!-- <el-table-column align="center" :label="$t('product.controller.list.productTitle')" min-width="100" prop="productTitle" /> -->

      <el-table-column align="center" :label="$t('product.controller.list.description')" min-width="100" prop="description" />

      <el-table-column align="center" :label="$t('product.controller.list.gmtCreate')" width="250" prop="gmtCreate">
        <template slot-scope="scope">{{ scope.row.gmtCreate | formatTime }}</template>
      </el-table-column>

      <el-table-column align="center" :label="$t('product.controller.list.operation')" width="250" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('product.controller.list.edit') }}</el-button>
          <el-button type="danger" style="margin:5px" size="mini" @click="handleDelete(scope.row)">{{ $t('product.controller.list.deletE') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-tooltip placement="top" :content="$t('product.controller.list.toTop')">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

    <el-dialog :visible.sync="dialogVisiable" :title="$t('product.controller.list.create')">
      <el-form
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item :label="$t('product.controller.list.title')" prop="title">
          <el-input v-model="createTitle" />
        </el-form-item>
        <el-form-item :label="$t('product.controller.list.description')" prop="description">
          <el-input v-model="createDescription" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiable = false">{{ $t('product.controller.list.cancel') }}</el-button>
        <el-button :loading="submitting" type="primary" @click="doCreate">{{ $t('product.controller.list.confirm') }}</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listProduct } from '@/api/product'
import { createController, listController } from '@/api/controller'
import BackToTop from '@/components/BackToTop'

export default {
  name: 'ControllerList',
  components: { BackToTop },
  data() {
    return {
      listQuery: {
        title: '',
        productId: undefined
      },
      listLoading: false,
      emptyText: this.$t('product.controller.list.emptyText'),
      list: [],
      productList: [],
      dialogVisiable: false,
      submitting: false,
      createTitle: '',
      createDescription: ''
    }
  },
  created() {
    const productId = this.$route.query.productId
    if (productId) {
      this.listQuery.productId = parseInt(productId)
      this.getList()
    }
    this.getProductList()
  },
  methods: {
    // 查询
    getProductList() {
      listProduct({
        pageSize: 15
      })
        .then(res => {
          this.productList = res.data.items
          if (res.data.total > 15) {
            // TODO 若总数超过了15，则转用可搜索下拉框
          }
        })
    },
    getList() {
      this.listLoading = true
      listController(this.listQuery.productId)
        .then(res => {
          this.list = res.data
          this.listLoading = false
        }).catch(failres => {
          this.listLoading = false
        })
    },
    handleFilter() {
      this.getList()
    },
    // 更新
    handleUpdate(row) {
      this.$router.push({ path: '/product/controller/upsert', query: { controllerId: row.id }})
    },
    // 删除
    handleDelete() {

    },
    // 增加
    handleCreate(row) {
      // 1. 判断产品是否被选中
      if (this.listQuery.productId) {
        this.dialogVisiable = true
      } else {
        this.$notify({
          title: this.$t('message.fail'),
          message: this.$t('product.controller.list.productNotSelected'),
          type: 'error'
        })
      }
    },
    doCreate() {
      if (!this.createTitle) {
        this.$notify({
          title: this.$t('message.fail'),
          message: this.$t('product.controller.list.titleCannotBeEmpty'),
          type: 'error'
        })
        return
      }
      this.submitting = true
      createController({
        title: this.createTitle,
        description: this.createDescription,
        productId: this.listQuery.productId
      }).then(res => {
        this.$notify({
          title: this.$t('message.success'),
          message: this.$t('product.controller.list.createControllerSuccess'),
          type: 'success'
        })
        this.submitting = false
        this.dialogVisiable = false
      }).catch(failres => {
        console.log(failres)
        this.submitting = false
      })
    }
  }
}
</script>
